﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;
using System.Collections.Generic;
using BOHVote.Common;
using System.IO;


namespace BOH.AccreditationSystem
{
    public partial class PPTMatchingCheck : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //ApplyListDataSet();
            }
        }

        public void ApplyListDataSet()
        {
            
            DataSet dst = dstApplyList("-1", "-1", txtApplyName.Text, "-1", "-1", "-1", "-1", string.Empty, (string)Session["ApplyPeriod"]);
            DataColumn c1 = new DataColumn("IsMatching");
            dst.Tables[0].Columns.Add(c1);
            DataColumn c2 = new DataColumn("PPTLink");
            dst.Tables[0].Columns.Add(c2);
            foreach(DataRow dr in dst.Tables[0].Rows)
            {
                if (!String.IsNullOrEmpty(dr["ApplyPPT"].ToString()))
                {
                    dr["IsMatching"] = "匹配";
                    dr["PPTLink"] = common.getAppSetting("FolderforLoadPath") + "/" + (string)Session["ApplyPeriod"] + "/" + dr["ApplyPPT"].ToString();
                }
                else
                {
                    dr["IsMatching"] = string.Empty;
                    dr["PPTLink"] = "#";
                }
            }
            BindApplyList(dst);
        }

        public DataSet dstApplyList(string UnitID, string GovernID, string ApplyName, string ApplyType, string SpecialityID, string SubjectID, string JobRank, string IDCard, string ApplyPeriod)
        {
            DataSet dsta = null;
            string UnitGovrenID = string.Empty;
            dsta = ApplyClass.dstApplys(UnitID, GovernID, ApplyName, ApplyType, SpecialityID, SubjectID, JobRank, IDCard, ApplyPeriod);
            return dsta;
        }

        public void BindApplyList(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    DataView dv = new DataView();
                    dv.Table = dst.Tables[0];
                    dv.Sort= "ApplyPPT";
                    pds.DataSource = dv;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    ApplyViewList.DataSource = pds;
                    ApplyViewList.DataBind();
                }
                else
                {
                    ApplyViewList.DataSource = null;
                    ApplyViewList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }
            }
            else
            {
                AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                ApplyViewList.DataSource = null;
                ApplyViewList.DataBind();
            }
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            ApplyListDataSet();
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            ApplyListDataSet();
        }

        protected void butMatching_Click(object sender, EventArgs e)
        {
            string strPeriod = (string)Session["ApplyPeriod"];
            string LoadFullPath = common.getAppSetting("FolderforSavePath") + @"\" + strPeriod;
            List<string> fileNames = new List<string>();
            if (Directory.Exists(LoadFullPath))
            {
                DirectoryInfo di = new DirectoryInfo(LoadFullPath);
                FileInfo[] FI = di.GetFiles();
                foreach (FileInfo f in FI)
                {
                    if (f.Extension.ToLower() == ".ppt" || f.Extension.ToLower() == ".pptx"||f.Extension.ToLower() == ".pps")
                        fileNames.Add(f.Name.ToLower());
                }

                RetrieveFile(fileNames);
            }

            ApplyListDataSet();
        }

        private void RetrieveFile(List<string> files)
        {
            string PPTFile = string.Empty;
            DataSet dst = dstApplyList("-1", "-1", txtApplyName.Text, "-1", "-1", "-1", "-1", string.Empty, (string)Session["ApplyPeriod"]);
            if (dst != null && dst.Tables.Count > 0 && dst.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    if (files.Contains(dr["IDCard"].ToString().Trim() + ".ppt"))
                    {
                        PPTFile = dr["IDCard"].ToString().Trim() + ".ppt";
                    }
                    else if (files.Contains(dr["IDCard"].ToString().Trim() + ".pptx"))
                    {
                        PPTFile = dr["IDCard"].ToString().Trim() + ".pptx";
                    }
                    else if (files.Contains(dr["IDCard"].ToString().Trim() + ".pps"))
                    {
                        PPTFile = dr["IDCard"].ToString().Trim() + ".pps";
                    }
                    else
                    {
                        PPTFile = string.Empty;
                    }
                    if (!String.IsNullOrEmpty(PPTFile))
                        ApplyClass.ApplyPPTUpdate(dr["ApplyID"].ToString().Trim(), PPTFile);
                }
            }
        }

    }
}
